#define NORFLASH_VAR

#include "NorFlash.h"

#include "willis.h"
#include "include.h"
#include "cs32f10x_spi.h"
#include "cs32f10x_dma.h"


const unsigned char number32_buf[11][64]={
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xC0,0x06,0x20,
0x0C,0x30,0x18,0x18,0x18,0x18,0x18,0x08,0x30,0x0C,0x30,0x0C,0x30,0x0C,0x30,0x0C,
0x30,0x0C,0x30,0x0C,0x30,0x0C,0x30,0x0C,0x30,0x0C,0x30,0x0C,0x18,0x08,0x18,0x18,
0x18,0x18,0x0C,0x30,0x06,0x20,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"0",0*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x01,0x80,
0x1F,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,
0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,
0x01,0x80,0x01,0x80,0x03,0xC0,0x1F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"1",1*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xE0,0x08,0x38,
0x10,0x18,0x20,0x0C,0x20,0x0C,0x30,0x0C,0x30,0x0C,0x00,0x0C,0x00,0x18,0x00,0x18,
0x00,0x30,0x00,0x60,0x00,0xC0,0x01,0x80,0x03,0x00,0x02,0x00,0x04,0x04,0x08,0x04,
0x10,0x04,0x20,0x0C,0x3F,0xF8,0x3F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"2",2*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xC0,0x18,0x60,
0x30,0x30,0x30,0x18,0x30,0x18,0x30,0x18,0x00,0x18,0x00,0x18,0x00,0x30,0x00,0x60,
0x03,0xC0,0x00,0x70,0x00,0x18,0x00,0x08,0x00,0x0C,0x00,0x0C,0x30,0x0C,0x30,0x0C,
0x30,0x08,0x30,0x18,0x18,0x30,0x07,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"3",3*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x60,
0x00,0xE0,0x00,0xE0,0x01,0x60,0x01,0x60,0x02,0x60,0x04,0x60,0x04,0x60,0x08,0x60,
0x08,0x60,0x10,0x60,0x30,0x60,0x20,0x60,0x40,0x60,0x7F,0xFC,0x00,0x60,0x00,0x60,
0x00,0x60,0x00,0x60,0x00,0x60,0x03,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"4",4*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xFC,0x0F,0xFC,
0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x13,0xE0,0x14,0x30,
0x18,0x18,0x10,0x08,0x00,0x0C,0x00,0x0C,0x00,0x0C,0x00,0x0C,0x30,0x0C,0x30,0x0C,
0x20,0x18,0x20,0x18,0x18,0x30,0x07,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"5",5*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xE0,0x06,0x18,
0x0C,0x18,0x08,0x18,0x18,0x00,0x10,0x00,0x10,0x00,0x30,0x00,0x33,0xE0,0x36,0x30,
0x38,0x18,0x38,0x08,0x30,0x0C,0x30,0x0C,0x30,0x0C,0x30,0x0C,0x30,0x0C,0x18,0x0C,
0x18,0x08,0x0C,0x18,0x0E,0x30,0x03,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"6",6*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFC,0x1F,0xFC,
0x10,0x08,0x30,0x10,0x20,0x10,0x20,0x20,0x00,0x20,0x00,0x40,0x00,0x40,0x00,0x40,
0x00,0x80,0x00,0x80,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x03,0x00,0x03,0x00,
0x03,0x00,0x03,0x00,0x03,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"7",7*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xE0,0x0C,0x30,
0x18,0x18,0x30,0x0C,0x30,0x0C,0x30,0x0C,0x38,0x0C,0x38,0x08,0x1E,0x18,0x0F,0x20,
0x07,0xC0,0x18,0xF0,0x30,0x78,0x30,0x38,0x60,0x1C,0x60,0x0C,0x60,0x0C,0x60,0x0C,
0x60,0x0C,0x30,0x18,0x18,0x30,0x07,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"8",8*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xC0,0x18,0x20,
0x30,0x10,0x30,0x18,0x60,0x08,0x60,0x0C,0x60,0x0C,0x60,0x0C,0x60,0x0C,0x60,0x0C,
0x70,0x1C,0x30,0x2C,0x18,0x6C,0x0F,0x8C,0x00,0x0C,0x00,0x18,0x00,0x18,0x00,0x10,
0x30,0x30,0x30,0x60,0x30,0xC0,0x0F,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"9",9*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x18,0x00,0x3C,0x00,0x3C,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*".",10*/
};

#if 1
void SPI_gpio_init()
{
	__RCU_APB2_CLK_ENABLE(RCU_APB2_PERI_GPIOA);// Enable  GPIO clocks 
	gpio_mode_config(GPIOA,  SPI_CS_PIN  ,  GPIO_MODE_OUT_PP(GPIO_SPEED_HIGH)); //CS
	gpio_mode_config(GPIOA,  SPI_MISO_PIN,  GPIO_MODE_IN_PU); //SD_SPI_MISO
	gpio_mode_config(GPIOA,  SPI_MOSI_PIN , GPIO_MODE_OUT_PP(GPIO_SPEED_HIGH)); //SD_SPI_MOSI 
    gpio_mode_config(GPIOA,  SPI_SCK_PIN ,  GPIO_MODE_OUT_PP(GPIO_SPEED_HIGH)); //SD_SPI_SCK
}
void send_data_spi(unsigned char dat)
{
	//printf("%x\t",dat);
//	unsigned int getvalue=0;
//	unsigned char value=0;
    char i;
    for(i=0;i<8;i++)
	{
		SCK_L;
		if(dat&0x80)
		{
			MOSI_H;
			//printf("H\t");
		} 
		else
		{
			MOSI_L;
			//printf("L\t");
		} 
		//mDelayuS(100);
		SCK_H;
		dat=dat<<1;
	}
	SCK_L;
	//printf("\n");
}

unsigned char read_data_spi(void)
{
	unsigned char getvalue=0;
	unsigned char value=0;
    unsigned char i;

	for(i=0;i<8;i++)
	{
		MOSI_H;
		SCK_L;
		if(ReadMISO()==SET)
		{
			getvalue= 1;//printf("H\t");
		}
		else              
		{
			getvalue= 0;//printf("L\t");
		}
		SCK_H;
		value<<=1;
		if(getvalue)
		{
			value |=0x01;
		}

	}
	MOSI_L;
	//printf("\n");
	return value;
}
  
unsigned char SpiFlash_ReadID()
{
	static uint32_t id;

	FLASH_SEL();
	send_data_spi(0x9f);
	flash_idbug[0]=read_data_spi();
	flash_idbug[1]=read_data_spi();
	flash_idbug[2]=read_data_spi();
	flash_idbug[3]=read_data_spi();
	 
	FLASH_DESEL();
	
	id = (((uint32_t)flash_idbug[0])<<16) | (((uint32_t)flash_idbug[1])<<8) |((uint32_t)flash_idbug[2]);
    printf("read ID is was:%X  %X  %X  0x%X\r\n",flash_idbug[0],flash_idbug[1],flash_idbug[2],id);
 
    if( (id & 0x00FFFF00) == 0xEF4000 )            // w25q128
		return 1;
	else if( id == 0xc22018 )       // s25l128s
		return 1;
	else if( id == 0xc84017 )       // GD25G64
		return 1;
	else 
		return 0;
}
void FlashWriteEn()
{
	{
		FLASH_SEL();
		send_data_spi(WREN);	
		FLASH_DESEL();
	}
}
void FlashWriteDis()
{
	{
		FLASH_SEL();
		send_data_spi(WRDI);	
		FLASH_DESEL();
	}
}
void FlashWriteUnlock()
{
	{
		FlashWriteEn();
		FLASH_SEL();
		send_data_spi(WRSR);
		send_data_spi(0x02);			
		FLASH_DESEL();
		FlashWriteDis();
	}
}
uint32_t FlashRDSR(void)
{
	static uint32_t status=0;
	FLASH_SEL();

	send_data_spi(RDSR);
	flash_idbug[0]=read_data_spi();
	flash_idbug[1]=read_data_spi();
	flash_idbug[2]=read_data_spi();
	 
	FLASH_DESEL();
	
	status = (((uint32_t)flash_idbug[0])<<16) | (((uint32_t)flash_idbug[1])<<8) |((uint32_t)flash_idbug[2]);
  	//printf("read status is was:%.2X  %.2X  %.2X  0x%X\r\n",flash_idbug[2],flash_idbug[1],flash_idbug[0],status);
	return status;
}

void FlashSE(unsigned char erase_command,unsigned int Addr)
{

	
		FlashWriteEn();
		FLASH_SEL();
 
		send_data_spi(erase_command); 

		send_data_spi((unsigned char)( (Addr>>16)&0xff));
		send_data_spi((unsigned char)( (Addr>>8)&0xff));
		send_data_spi((unsigned char)( (Addr & 0x000000ff )));
	 	 FLASH_DESEL();

		//while( ((FlashRDSR()&BUSY_MASK) ) );
		op_timeout = 5000;
		while( op_timeout )
		{
			op_timeout--;
			if(op_timeout==0) printf("\n erase flash over \n"); //超时退出
			if( (FlashRDSR()&BUSY_MASK) ) ; 					//等待完成
			else                		  op_timeout=0;			//完成直接退出          
		}
		FlashWriteDis();
	
}

void FlashWrite(unsigned int Addr,unsigned char* sdat,int length)
{
	int i;
 
    FlashWriteEn();
	FLASH_SEL();
 
	send_data_spi( PP);
 
	send_data_spi((unsigned char)( (Addr>>16)&0xff));
	send_data_spi((unsigned char)( (Addr>>8)&0xff));
	send_data_spi((unsigned char)( (Addr & 0x000000ff )));

	for( i=0;i<length;i++)
		send_data_spi(*(sdat+i));
	FLASH_DESEL();
	
	//while( ( (FlashRDSR()&BUSY_MASK) ) ) ;
	op_timeout = 5000;
	while( op_timeout )
	{
		op_timeout--;
		if(op_timeout==0) printf("\n w flash over \n");//超时退出
		if( (FlashRDSR()&BUSY_MASK) ) ; 			   //等待完成
		else                		  op_timeout=0;    //完成直接退出        
	}
	FlashWriteDis();
 
}
void FlashRead(unsigned int Addr,unsigned char* rdat,int length)
{
	unsigned int m = 0;
	{

		FLASH_SEL();
		
		send_data_spi(FAST_READ); 
		
		send_data_spi((unsigned char)( (Addr>>16)&0xff));
		send_data_spi((unsigned char)( (Addr>>8)&0xff));
		send_data_spi((unsigned char)( (Addr & 0x000000ff )));		
		send_data_spi(0xff);		//  dummy byte
		#if 1
		for(m=0;m<length;m++)
		{
			*(rdat+m)=read_data_spi();
		}
		#endif
		FLASH_DESEL();
 
	}
}
#endif
 
void show_number(uint8_t x,uint8_t y,uint8_t number,uint16_t color)
{
	uint16_t i, j;
	uint8_t temp=0x80;
	if(number<11)
	{
		for (i = 0; i < 32; i++)
		{
			temp=0x80;
			for (j = 0; j < 8; j++)
			{
				if(number32_buf[number][2*i]&temp)
				{
					LCD_DrawPoint(x+j,y+i,color);
				}
				else
				{
					LCD_DrawPoint(x+j,y+i,0);
				}
				temp>>=1;
			}
			temp=0x80;
			for (j = 0; j < 8; j++)
			{
				if(number32_buf[number][2*i+1]&temp)
				{
					LCD_DrawPoint(x+j+8,y+i,color);
				}
				else
				{
					LCD_DrawPoint(x+j+8,y+i,0);
				}
				temp>>=1;
			}
		}
	}
	else if(number<100)
	{
	}
	else
	{
	}
}



//void display_picture(uint8_t number)
//{
//		uint16_t i=0;
//	for(i=0;i<30;i++)
//	{
//		Spi1ReadFlashData(0x1e5280+i*3840+number*115200,3840,0);
//		LCD_write_buf(0, 8*i, &spi0_recieveA[5] ,3840);
//		SendDataSPIDMA(&rbuf[5],3840);	
//	}
//}



